package com.bhs.sansonglogistics.view.banner;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;


import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.AdvertisingBean;
import com.bhs.sansonglogistics.bean.SlideshowBean;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.WatermarkUtils;
import com.bhs.sansonglogistics.view.Watermark;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends PagerAdapter {
    private List<SlideshowBean> drawableList = new ArrayList<>();
    private Context mContext;
    /**
     * 缓存view
     */
    private SparseArray<View> mCacheView;

    //是否显示水印
    private boolean isShowWatermark;
    //是否显示圆角
    private int isRoundedCorner;

    //是否显示完整显示FIT_XY
    private final boolean isFull;

    public ViewPagerAdapter(List<SlideshowBean> list, Context context, boolean isShowWatermark, boolean isFull) {
        clear();
        drawableList = list;
        mContext = context;
        this.mCacheView = new SparseArray<>();
        this.isShowWatermark = isShowWatermark;
        this.isFull = isFull;
    }

    public ViewPagerAdapter(List<SlideshowBean> list, Context context, boolean isFull) {
        clear();
        drawableList = list;
        mContext = context;
        this.mCacheView = new SparseArray<>();
        this.isFull = isFull;
    }

    public ViewPagerAdapter(List<SlideshowBean> list, Context context, int isRoundedCorner, boolean isFull) {
        clear();
        drawableList = list;
        mContext = context;
        this.mCacheView = new SparseArray<>();
        this.isRoundedCorner = isRoundedCorner;
        this.isFull = isFull;
    }


    public void clear() {
        if (null != mCacheView) {
            mCacheView.clear();
            mCacheView = null;
        }
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        View contentView = mCacheView.get(position);
        if (contentView == null) {
            int layoutId;
            if (isRoundedCorner == 1) {
                layoutId = R.layout.layout_banner;
            } else {
                layoutId = R.layout.layout_banner_two;
            }
            contentView = LayoutInflater.from(container.getContext()).inflate(layoutId, null);
            ImageView iv_bg = contentView.findViewById(R.id.iv_bg);
            if (isFull) {
                iv_bg.setScaleType(ImageView.ScaleType.FIT_XY);
            }
            String picUrl = drawableList.get(position % drawableList.size()).getPicurl();
            ImageView ivPlay = contentView.findViewById(R.id.iv_play);
            if (drawableList.get(position % drawableList.size()).getType() == 2) {
                ivPlay.setVisibility(View.VISIBLE);
            } else {
                ivPlay.setVisibility(View.GONE);
            }
            Glide.with(mContext).load(picUrl).into(iv_bg);
            if (isShowWatermark) {
                Watermark.getInstance().show(mContext, contentView.findViewById(R.id.rl_parent));
            }


            iv_bg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnItemClick != null) {
                        iOnItemClick.onItemClick(position % drawableList.size());
                    }
                }
            });
            mCacheView.put(position, contentView);
        }
        (container).addView(contentView, 0);
        contentView.setFocusable(false);
        return contentView;

    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        (container).removeView((View) object);
        mCacheView.remove(position);
    }

    public IOnItemClick iOnItemClick;

    public void setIOnItemClick(IOnItemClick iOnItemClick) {
        this.iOnItemClick = iOnItemClick;
    }

    public interface IOnItemClick {
        void onItemClick(int position);
    }
}

